package com.yj.springbootmuldatasources.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@MapperScan(value = "com.yj.springbootmuldatasources.dao.dev",sqlSessionFactoryRef = "devSqlSessionFactory")
public class DevMybatisConfig {


    @Bean
    public SqlSessionFactory devSqlSessionFactory(DataSource devDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("mapper/dev/*.xml"));
        sqlSessionFactoryBean.setDataSource(devDataSource);
        return sqlSessionFactoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate devSqlSessionTemplate(SqlSessionFactory devSqlSessionFactory){
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(devSqlSessionFactory);
        return sqlSessionTemplate;
    }

}
